package com.bear.cmcc.NetTestHTTP.ui.activity;

import com.bear.cmcc.NetTestHTTP.R;
import com.bear.cmcc.NetTestHTTP.constant.MyCommonConstant;
import com.bear.cmcc.NetTestHTTP.service.NetUpdateVersionService;
import com.bear.cmcc.NetTestHTTP.ui.view.LoadingDialog;
import com.bear.cmcc.NetTestHTTP.util.SetUtil;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

/**
 * ���ڽ���
 * 
 * @author jqa
 * 
 */
public class AboutActivity extends AbstractActivity {
	private TextView tvNowVersion;
	private Button btnCheckUpdate;
	private ApkInstallReceiver receiver=new ApkInstallReceiver();
	private LoadingDialog dialog;
	
    private Message message=null;
    
    int refresh=1;
    private Handler refreshHandler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            if (msg.what == refresh) {
                String message=(String)msg.obj;
                if(dialog!=null)
                {
                    dialog.updateMessage(message);
                }
                
            }
        }
    };
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
//		setCloseButton(R.string.top_close, buttonListener);
		addRightCloseButton();
		setTitle(R.string.aboutus);// ����
		SetBottomBarVisible(false);
		setContentView(R.layout.activity_about);
		dialog = new LoadingDialog(this);
		
		IntentFilter filter=new IntentFilter();
		filter.addAction(MyCommonConstant.PrivateActionName.NET_WORK_APK_DATA_DOWN);
		 filter.addAction(MyCommonConstant.PrivateActionName.DOWN_PROCESS_ACTION);
		registerReceiver(receiver, filter);
		init();
	}

	private void init() {
		tvNowVersion = (TextView) findViewById(R.id.textNowVersion);
		tvNowVersion.setText(getString(R.string.nowVersion) + "V"
				+ SetUtil.getLocalVersion(this)[1]);
		btnCheckUpdate = (Button) findViewById(R.id.btnCheckUpdate);
		btnCheckUpdate.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				dialog.show();
				Toast.makeText(getBaseContext(), "正在检查新版本...", 0).show();
				Intent intent = new Intent(AboutActivity.this,NetUpdateVersionService.class);
				stopService(intent);
				SetUtil.updateNewVersion_FJ(AboutActivity.this,false,null);
			}
		});
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		try {
            unregisterReceiver(receiver);
            if(refreshHandler!=null)
            {
                refreshHandler.removeMessages(refresh);
                refreshHandler=null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
	}



	private OnClickListener buttonListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			finish();
		}
	};

	class ApkInstallReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
		    if (MyCommonConstant.PrivateActionName.DOWN_PROCESS_ACTION.equals(intent.getAction())) {
                String process = intent
                        .getStringExtra(MyCommonConstant.PrivateActionName.DOWN_PROCESS);
                String total = intent
                        .getStringExtra(MyCommonConstant.PrivateActionName.DOWN_PROCESS_TOTAL);
                message=new Message();
                message.what=refresh;
                message.obj=String.format(context.getString(R.string.processing), process,total);
                refreshHandler.sendMessage(message);
                
            } else
			if(intent.getAction().equals(MyCommonConstant.PrivateActionName.NET_WORK_APK_DATA_DOWN)){
					String type = intent.getStringExtra(MyCommonConstant.PrivateActionName.NET_WORK_APK_DATA_DOWN_TYPE);
					if(type.equals(MyCommonConstant.PrivateActionName.NET_WORK_APK_DATA_DOWN_TYPE_CHECK)){
						dialog.dismiss();
						//版本更新的反馈
						String updateStatus = intent.getStringExtra(MyCommonConstant.PrivateActionName.NET_WORK_APK_ISUPDATE);
						if(updateStatus.equals(MyCommonConstant.PrivateActionName.NET_WORK_APK_ISUPDATE_YES)){
							//需要更新
							String Description = intent.getStringExtra(MyCommonConstant.PrivateActionName.ALL_APK_DOWNSTATUS);
							String downUrl = intent.getStringExtra(MyCommonConstant.PrivateActionName.ALL_APK_DOWN_URL);
							SetUtil.openIsUpdateVerion_FJ_Close(context,Description,dialog,false,downUrl,true);
						}else{
							String status = intent.getStringExtra(MyCommonConstant.PrivateActionName.ALL_APK_DOWNSTATUS);
							if("1".equals(status)){
								Toast.makeText(getBaseContext(), "已经是最新版本", Toast.LENGTH_SHORT).show();
							}else{
								Toast.makeText(getBaseContext(), "获取版本信息失败", Toast.LENGTH_SHORT).show();
							}
							Intent intentService = new Intent(AboutActivity.this, NetUpdateVersionService.class);
							stopService(intentService);
						}
					}else if(type.equals(MyCommonConstant.PrivateActionName.NET_WORK_APK_DATA_DOWN_TYPE_DOWN)){
						//下载的反馈
						String downStatus = intent.getStringExtra(MyCommonConstant.PrivateActionName.ALL_APK_DOWNSTATUS);
						if(downStatus.equals(MyCommonConstant.PrivateActionName.ALL_APK_DOWNSTATUS_YES)){
							//下载成功
							dialog.dismiss();
							SetUtil.installNewVersion_FJ(AboutActivity.this,false);
						}else{
							//下载失败
							dialog.dismiss();
							Toast.makeText(getBaseContext(), "下载安装文件失败", Toast.LENGTH_SHORT).show();
						}
						Intent intentService = new Intent(AboutActivity.this, NetUpdateVersionService.class);
						stopService(intentService);
					}
			}
		}
	}
}
